////////////////////////////////////////////////
// DESCRIPTION:
//    Utility Functions 
//
// Legal Notices:
//    Copyright (C) 2005, EmbeddedFusion
//    ALL RIGHTS RESERVED
//
//    This file, software, or program is supplied under the terms of a
//    license agreement and/or nondisclosure agreement with EmbeddedFusion
//    and may not be copied or disclosed except in accordance with the
//    terms of that agreement.  This file, software, or program contains
//    copyrighted material and/or trade secret information of EmbeddedFusion,
//    and must be treated as such. EmbeddedFusion reserves all rights in this
//    material, except as the license agreement or nondisclosure agreement
//    specifically indicate.
//
//    All rights reserved.  No part of this program or publication may be
//    reproduced, transmitted, transcribed, stored in a retrieval system
//    retrieval system, or translated into any language or computer
//    language, in any form or by any means, electronic, mechanical,
//    magnetic, optical, chemical, manual, or otherwise, without the prior
//    written permission of EmbeddedFusion.
//
// Disclaimer:
//    EmbeddedFusion makes no warranty of any kind regarding this code.
//    This code is provided on an "As Is" basis and EmbeddedFusion will
//    not provide any support assistance, installation, training or other
//    services.
//    EmbeddedFusion does not provide any updates, enhancements or extensions.
//    EmbeddedFusion specifically disclaims any warranty of merchantability,
//    non-infringement, fitness for any particular purpose,
//    or any other warranty.
//
//    EmbeddedFusion disclaims all liability, including liability
//    for infringement of any proprietary rights, relating to use of the
//    code. No license, express or implied, by estoppel or otherwise,
//    to any intellectual property rights is granted herein.
//
//    EmbeddedFusion has no responsibility under warranty, indemnification
//    or otherwise, for the modification or customization of any EmbeddedFusion
//    software by EmbeddedFusion, customer, website user or any third party even
//    if such customization and/or modification is done using EmbeddedFusion tools,
//    training or methods documented by EmbeddedFusion.
//
#pragma once

#include <wtypes.h>
#include "Utilities\BitFlags.h"
#include <FusionWare\SafeHalEther.h>
#include "Utilities\EnterExitMsg.h"

//////////////////////////////////
// Determines if the code is running in a power
// handler context. In normal OS code this
// just uses the OS provided IsInPwrHdlr()
// for the boot loader it always returns TRUE
//
extern "C" BOOL InPowerHandler();

// This header is used by some older/ported C code
// primarily to get the bit flags
#ifdef __cplusplus
#include "Utilities\FillMem.h"

namespace FusionWare
{
    // <<Utility>>
    class Utilities
    {
    public:
        // Table containing numbers of days in each month 
        // MonthTable[0][...] is for non leap years.
        // MonthTable[1][...] is for leap years.
        static const unsigned int MonthTable[2][12];

        ///////////////////////////////////////////////////////
        // Description: 
        //    Checks if a year is a leap year
        //
        // Input: 
        //    year - Year to check
        //
        // Returns: 
        //     int - 0 for non leap year 1 if it is a leap year. 
        //
        // Remarks:
        //     If year is divisible by 4 it probably is a leap
        //     year but it *might *not be! This function checks
        //     for the century rules.
        //
        //     The return value is intentionally *NOT* a bool
        //     as it is often used in mathematical expressions
        //     as an integer to get the number of days in the
        //     year.
        //
        static int IsLeapYear(int year);

        ///////////////////////////////////////////////////////
        // Description: 
        //    Verifies a given system time is valid
        // 
        // Input: 
        //    pSysTime - Time to vliadate
        //
        // Returns: 
        //     bool 
        //     
        // Remarks: 
        //     
        // Note: 
        //     
        static bool IsDateValid(const SYSTEMTIME* pSysTime);

        ///////////////////////////////////////////////////////
        // Description: 
        //    Creates a Device Name for KITL Ethernet and EBOOT 
        //    connectivity
        //
        // Input: 
        //    MyAddr       - MAC address
        //    pBuf         - Buffer to place the string into 
        //    PlatformName - Name of the platform to use in creating
        //                   the unique device name.
        //
        // Returns: 
        //     void 
        //     
        // Remarks: 
        //    Uses BSP::PlatformsStrW and MyAddr to create a
        //    unique name for the device.
        //
        static void CreateDeviceName(const EDBG_ADDR& MyAddr, char* pBuf, const char* PlatformName);

        ///////////////////////////////////////////////////////
        // Description: 
        //    Delays execution for a specified time (microseconds)
        //
        // Input: 
        //    MicroSeconds - number of microseconds to spin
        //
        // Remarks: 
        //    spins in a loop for a specified amount of time
        //    typically uses the system timer to determine the
        //    time.
        //
        static void uDelay(UINT32 MicroSeconds);

        ///////////////////////////////////////////////////////
        // Description: 
        //    Dumps binary data to EdbgOutputDebugString
        //
        // Input: 
        //    pData   The Data
        //    Length  Length of the data
        //
        static void DumpData( const BYTE *pData, size_t Length );

        ///////////////////////////////////////////////////////
        // Description: 
        //    Debug dump of an Ethernet frame
        //
        // Input: 
        //    *pFrame     - Pointer to Ethernet frame to display
        //    FrameLength - Length of the frame
        //
        static void DumpEtherFrame( const BYTE *pFrame, WORD FrameLength );
        

        ///////////////////////////////////////////////////////
        // Description: 
        //    Convert any value to a printable ASCII character
        //
        // Input: 
        //    c - Value to convert
        //
        // Returns: 
        //    Printable char
        //     
        // Remarks: 
        //    Any non printable characters are converted to a "."
        //    otherwise c is returned unaltered.
        //
        // Note: 
        //     
        static char PrintChar(BYTE c)
        {
            return (c>=0x20 && c<=0x7E)?c:'.';
        }

        static void WriteChar(char c);
        static void WriteWideChar(WCHAR c);

        // Calculate a checksum
        static UINT16 Sum(UINT16 seed, VOID *pData, UINT32 size);

    };
}
#endif